package symbolClass;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

/*
    每层嵌套一个segment
    seg里装了变量/过程
    2022-3-11-杨海南
 */
public class Segment {
    private final HashMap<String, SymbolNode> symSeg = new HashMap<>();
    private final HashMap<String, FunctionNode> funcSeg = new HashMap<>();
    private final HashMap<String, TypeDeclaNode> typeSeg = new HashMap<>();
    private final Segment prevSeg;

    public Segment getPrevSeg() {
        return prevSeg;
    }

    public Segment(Segment prevSeg) {
        this.prevSeg = prevSeg;
    }

    public SymbolNode getSymbol(String variableName) {
        return symSeg.get(variableName);
    }

    public FunctionNode getFunction(String functionName) {
        return funcSeg.get(functionName);
    }

    public TypeDeclaNode getType(String typeName) {
        return typeSeg.get(typeName);
    }

    public void addSymbol(String variableName, SymbolNode symbol) {
        symSeg.put(variableName, symbol);
    }

    public void addFunction(String functionName, FunctionNode functionNode) {
        funcSeg.put(functionName, functionNode);
    }

    public void addType(String typeName, TypeDeclaNode typeNode) {
        typeSeg.put(typeName, typeNode);
    }

    //判断这个块中有没有存在这个符号（变量or函数）
    public boolean containsSymbol(String symbolName) {
        return symSeg.containsKey(symbolName) || funcSeg.containsKey(symbolName) || typeSeg.containsKey(symbolName);
    }

    //仅判断符号
    public boolean containsVariable(String variableName) {
        return symSeg.containsKey(variableName);
    }

    //仅判断函数
    public boolean containsFunction(String functionName) {
        return funcSeg.containsKey(functionName);
    }

    public boolean containsType(String TypeName) {
        return typeSeg.containsKey(TypeName);
    }

    public Set<String> getVariableSet() {
        return symSeg.keySet();
    }

    public HashSet<SymbolNode> getEntrySet() {
        HashSet<SymbolNode> entrySet = new HashSet<>();
        entrySet.addAll(symSeg.values());
        return entrySet;
    }
}
